package controller;

import com.google.common.collect.ImmutableMap;
import common.pojo.CommonResult;
import common.spring.redis.RedisUnits;
import common.utils.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import pojo.WxUserInfo;
import service.WxUserFormIdService;
import utils.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

/**
 * Created by asus on 2019/1/30.
 */
@Controller
@RequestMapping("/wx")
public class WxUserFormIdController {

    @Autowired
    WxUserFormIdService idService;
    @Autowired
    RedisUnits redisUnits;
    private final Logger LOG = LoggerFactory.getLogger(WxUserFormIdController.class);
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public void addWxUserFormId(
            HttpServletRequest request,
            @RequestParam(value = "formId", required = false) String formId
    ) {
        if (StringUtils.isBlank(formId)) {
            return;
        }
        LOG.warn("---formId--"+formId);
        String sessionId = request.getHeader("sessionId");
        if (org.apache.commons.lang.StringUtils.isBlank(sessionId)) {
            return;
        }
        Map map = redisUnits.getMap(sessionId);
        if (map == null || map.size() == 0) {
            return;
        }
        String openId = map.get("openId").toString();
        if (StringUtils.isBlank(openId)) {
            return;
        }
        Map param= ImmutableMap.of("openId",openId,"formId",formId,"createTime",new Date());
        LOG.warn("---formId--"+JsonUtils.toJson(param));
        idService.addWxUserFormId(param);
    }
}
